<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php function threadedComments($comments, $options) {
    $commentClass = '';
    if ($comments->authorId) {
        if ($comments->authorId == $comments->ownerId) {
            $commentClass .= ' comment-by-author';
        } else {
            $commentClass .= ' comment-by-user';
        }
    }

    $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
?>
<li id="<?php $comments->theId(); ?>" class="comment comment-body<?php 
if ($comments->levels > 0) {
    echo ' comment-child';
    $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
} else {
    echo ' parent';
}
$comments->alt(' odd', ' even');
echo $commentClass;
?>">
<div id="<?php $comments->theId(); ?>" class="comment-body">
<div class="comment-author">
<img loading="lazy" class="avatar" src="<?php echo getAvatarByMail($comments->mail, $comments->authorId); ?>">
<cite class="fn"><?php $comments->author(); ?><?php if ($comments->authorId == $comments->ownerId) { ?> (作者)<?php }?></cite>
</div>
<time><?php $comments->date('Y-m-d H:i'); ?></time>
<div class="comment-content">
<?php echo get_comment_at($comments->coid); ?>
<?php $comments->content(); ?>
</div>
<div class="comment-reply"><?php $comments->reply('回复'); ?></div>
</div>
<?php if ($comments->children) { ?>
<div class="comment-children">
<?php $comments->threadedComments($options); ?>
</div>
<?php } ?>
</li>
<?php } ?>
<div id="comments">
<h2>评论</h2>
<?php $this->comments()->to($comments); ?>
<?php if ($comments->have()): ?>
<?php $comments->listComments(); ?>
<?php $comments->pageNav('<', '>', 1, '...', array('wrapTag' => 'nav', 'wrapClass' => 'reade_more','itemTag' => '','currentClass' => 'current')); ?>
<?php endif; ?>
<?php if($this->allow('comment')): ?>
<div id="<?php $this->respondId(); ?>" class="respond">
<h3 id="response">发表评论<small><?php $comments->cancelReply(); ?></small></h3>
<form method="post" action="<?php $this->commentUrl() ?>" id="commentform" role="form">
<?php if($this->user->hasLogin()): ?>
<p><?php _e('登录身份: '); ?><a href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>. <a href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> &raquo;</a></p>
<?php else: ?>
<p>
<label for="author" class="required"><?php _e('昵称 *'); ?></label>
<input type="text" name="author" id="author" class="text" value="" required />
</p>
<p>
<label for="mail"<?php if ($this->options->commentsRequireMail): ?> class="required"<?php endif; ?>><?php _e('邮箱 *'); ?></label>
<input type="email" name="mail" id="mail" class="text" value=""<?php if ($this->options->commentsRequireMail): ?> required<?php endif; ?> />
</p>
<p>
<label for="url"<?php if ($this->options->commentsRequireURL): ?> class="required"<?php endif; ?>><?php _e('网址'); ?></label>
<input type="url" name="url" id="url" class="text" placeholder="<?php _e('https://'); ?>" value=""<?php if ($this->options->commentsRequireURL): ?> required<?php endif; ?> />
</p>
<?php endif; ?>
<p>
<label for="textarea" class="required"><?php _e('评论'); ?></label>
<textarea rows="8" cols="50" name="text" id="textarea" class="textarea" required ><?php $this->remember('text'); ?></textarea>
</p>
<p>
<button type="submit" id="submit" class="submit">提交评论</button>
</p>
</form>
</div>
<?php else: ?>
<h3><?php _e('评论已关闭'); ?></h3>
<?php endif; ?>
</div>
<?php if(!$this->user->hasLogin()){ ?>
<script>
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
		return unescape(decodeURI(arr[2]));
	else
		return null;
	}
function adduser(){
	document.getElementById('author').value = getCookie('<?php echo md5($this->request->getUrlPrefix()); ?>__typecho_remember_author');
	document.getElementById('mail').value = getCookie('<?php echo md5($this->request->getUrlPrefix()); ?>__typecho_remember_mail');
	document.getElementById('url').value = getCookie('<?php echo md5($this->request->getUrlPrefix()); ?>__typecho_remember_url');
}
adduser();
</script>
<?php } ?>